<?php
define("EW_PAGE_ID", "update", TRUE); // Page ID
define("EW_TABLE_NAME", 'tb_locations', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "tb_locationsinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "tb_usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$tb_locations->Export = @$_GET["export"]; // Get export parameter
$sExport = $tb_locations->Export; // Get export parameter, used in header
$sExportFile = $tb_locations->TableVar; // Get export file, used in header
?>
<?php
$arRecKeys = array();
$nKeySelected = 0;
$sFilter = "";

// Try to load keys from list form
$nKeySelected = 0;
if (ew_IsHttpPost()) {
	if (isset($_POST["key_m"])) { // Key count > 0
		$nKeySelected = count($_POST["key_m"]); // Get number of keys
		$arRecKeys = ew_StripSlashes($_POST["key_m"]);
		LoadMultiUpdateValues(); // Load initial values to form
	}
}

// Try to load key from update form
if ($nKeySelected == 0) {
	$arRecKeys = array();

	// Create form object
	$objForm = new cFormObj();
	if (@$_POST["a_update"] <> "") {

		// Get action
		$tb_locations->CurrentAction = $_POST["a_update"];

		// Get record keys
		$sKey = @$_POST["k" . strval($nKeySelected+1) . "_key"];
		while ($sKey <> "") {
			$arRecKeys[$nKeySelected] = ew_StripSlashes($sKey);
			$nKeySelected++;
			$sKey = @$_POST["k" . strval($nKeySelected+1) . "_key"];
		}
		LoadFormValues(); // Get form values
	}
}
if ($nKeySelected <= 0) Page_Terminate("tb_locationslist.php"); // No records selected, return
switch ($tb_locations->CurrentAction) {
	case "U": // Update
		if (UpdateRows()) { // Update Records based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Set update success message
			Page_Terminate("tb_locationslist.php"); // Return to list page
		} else {
			RestoreFormValues(); // Restore form values
		}
}

// Render row
$tb_locations->RowType = EW_ROWTYPE_EDIT; // Render edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "update"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	if (!ew_UpdateSelected()) {
		alert('No field selected for update');
		return false;
	}
	var uelm;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_LocationName"];
		uelm = fobj.elements["u" + infix + "_LocationName"];
		if (uelm && uelm.checked) {
			if (elm && !ew_HasValue(elm)) {
				if (!ew_OnError(elm, "Please enter required field - Location Name"))
				return false;
			}
		}
		elm = fobj.elements["x" + infix + "_LocationRent"];
		uelm = fobj.elements["u" + infix + "_LocationRent"];
		if (uelm && uelm.checked) {
			if (elm && !ew_CheckNumber(elm.value)) {
				if (!ew_OnError(elm, "Incorrect floating point number - Location Rent"))
				return false; 
			}
		}
		elm = fobj.elements["x" + infix + "_PayRoll"];
		uelm = fobj.elements["u" + infix + "_PayRoll"];
		if (uelm && uelm.checked) {
			if (elm && !ew_CheckNumber(elm.value)) {
				if (!ew_OnError(elm, "Incorrect floating point number - Pay Roll"))
				return false; 
			}
		}
		elm = fobj.elements["x" + infix + "_Utilities"];
		uelm = fobj.elements["u" + infix + "_Utilities"];
		if (uelm && uelm.checked) {
			if (elm && !ew_CheckNumber(elm.value)) {
				if (!ew_OnError(elm, "Incorrect floating point number - Utilities"))
				return false; 
			}
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<script language="javascript">
<!--

function ew_SelectAll(bChecked)	{
	var f = document.ftb_locationsupdate;
	var sCtrl = "u";
	for (i=0; i<f.elements.length; i++) {
		var elm = f.elements[i];
		if (elm.type == "checkbox" && elm.name.substr(0, sCtrl.length+1) == sCtrl + "_") {
			elm.checked = bChecked;
		}
	}
}

function ew_UpdateSelected() {
	var f = document.ftb_locationsupdate;
	var sCtrl = "u";
	for (i=0; i<f.elements.length; i++) {
		var elm = f.elements[i];
		if (elm.type == "checkbox" && elm.name.substr(0, sCtrl.length+1) == sCtrl + "_") {
			if (elm.checked) return true;
		}
	}
	return false;
}

//-->
</script>
<p><span class="phpmaker">Update TABLE: Locations<br><br><a href="tb_locationslist.php">Back to List</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="ftb_locationsupdate" id="ftb_locationsupdate" action="tb_locationsupdate.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_update" id="a_update" value="U">
<?php for ($i = 0; $i < $nKeySelected; $i++) { ?>
<input type="hidden" name="k<?php echo $i+1 ?>_key" id="key<?php echo $i+1 ?>" value="<?php echo ew_HtmlEncode($arRecKeys[$i]) ?>">
<?php } ?>
<table class="ewTable">
	<tr class="ewTableHeader">
		<td>Update<input type="checkbox" value="" onClick="ew_SelectAll(this.checked);"></td>
		<td>Field Name</td>
		<td>New Value</td>
	</tr>
	<tr class="ewTableRow">
		<td<?php echo $tb_locations->LocationName->CellAttributes() ?>>
<input type="checkbox" name="u_LocationName" id="u_LocationName" value="1"<?php echo ($tb_locations->LocationName->MultiUpdate == "1")?" checked":"" ?>>
</td>
		<td<?php echo	$tb_locations->LocationName->CellAttributes() ?>>Location Name</td>
		<td<?php echo $tb_locations->LocationName->CellAttributes() ?>><span id="cb_x_LocationName">
<input type="text" name="x_LocationName" id="x_LocationName" title="" size="30" maxlength="45" value="<?php echo $tb_locations->LocationName->EditValue ?>"<?php echo $tb_locations->LocationName->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td<?php echo $tb_locations->LocationRent->CellAttributes() ?>>
<input type="checkbox" name="u_LocationRent" id="u_LocationRent" value="1"<?php echo ($tb_locations->LocationRent->MultiUpdate == "1")?" checked":"" ?>>
</td>
		<td<?php echo	$tb_locations->LocationRent->CellAttributes() ?>>Location Rent</td>
		<td<?php echo $tb_locations->LocationRent->CellAttributes() ?>><span id="cb_x_LocationRent">
<input type="text" name="x_LocationRent" id="x_LocationRent" title="" size="30" value="<?php echo $tb_locations->LocationRent->EditValue ?>"<?php echo $tb_locations->LocationRent->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td<?php echo $tb_locations->PayRoll->CellAttributes() ?>>
<input type="checkbox" name="u_PayRoll" id="u_PayRoll" value="1"<?php echo ($tb_locations->PayRoll->MultiUpdate == "1")?" checked":"" ?>>
</td>
		<td<?php echo	$tb_locations->PayRoll->CellAttributes() ?>>Pay Roll</td>
		<td<?php echo $tb_locations->PayRoll->CellAttributes() ?>><span id="cb_x_PayRoll">
<input type="text" name="x_PayRoll" id="x_PayRoll" title="" size="30" value="<?php echo $tb_locations->PayRoll->EditValue ?>"<?php echo $tb_locations->PayRoll->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td<?php echo $tb_locations->Utilities->CellAttributes() ?>>
<input type="checkbox" name="u_Utilities" id="u_Utilities" value="1"<?php echo ($tb_locations->Utilities->MultiUpdate == "1")?" checked":"" ?>>
</td>
		<td<?php echo	$tb_locations->Utilities->CellAttributes() ?>>Utilities</td>
		<td<?php echo $tb_locations->Utilities->CellAttributes() ?>><span id="cb_x_Utilities">
<input type="text" name="x_Utilities" id="x_Utilities" title="" size="30" value="<?php echo $tb_locations->Utilities->EditValue ?>"<?php echo $tb_locations->Utilities->EditAttributes() ?>>
</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="  Update  ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load initial values to form if field values are identical in all selected records
function LoadMultiUpdateValues() {
	global $arRecKeys, $nKeySelected, $tb_locations;

	// Build sql
	$sWrkFilter = "";
	for ($i = 0; $i < $nKeySelected; $i++) {
		$sFilter = $tb_locations->SqlKeyFilter();
		$sKey = trim($arRecKeys[$i]);
		$sKeyFld = $sKey;
		if (!is_numeric($sKeyFld)) return;
		$sFilter = str_replace("@ID@", ew_AdjustSql($sKeyFld), $sFilter); // Replace key value
		if ($sWrkFilter <> "") $sWrkFilter .= " OR ";
		$sWrkFilter .= $sFilter;
	}
	$tb_locations->CurrentFilter = $sWrkFilter;

	// Load recordset
	$rs = LoadRecordset();
	$i = 1;
	while (!$rs->EOF) {
		if ($i == 1) {
			$tb_locations->LocationName->setDbValue($rs->fields('LocationName'));
			$tb_locations->LocationRent->setDbValue($rs->fields('LocationRent'));
			$tb_locations->PayRoll->setDbValue($rs->fields('PayRoll'));
			$tb_locations->Utilities->setDbValue($rs->fields('Utilities'));
		} else {
			if (!ew_CompareValue($tb_locations->LocationName->DbValue, $rs->fields('LocationName'))) {
				$tb_locations->LocationName->CurrentValue = NULL;
			}
			if (!ew_CompareValue($tb_locations->LocationRent->DbValue, $rs->fields('LocationRent'))) {
				$tb_locations->LocationRent->CurrentValue = NULL;
			}
			if (!ew_CompareValue($tb_locations->PayRoll->DbValue, $rs->fields('PayRoll'))) {
				$tb_locations->PayRoll->CurrentValue = NULL;
			}
			if (!ew_CompareValue($tb_locations->Utilities->DbValue, $rs->fields('Utilities'))) {
				$tb_locations->Utilities->CurrentValue = NULL;
			}
		}
		$i++;
		$rs->MoveNext();
	}
	$rs->Close();
}

// Update all selected rows
function UpdateRows() {
	global $conn, $nKeySelected, $arRecKeys, $tb_locations;
	$conn->BeginTrans();

	// Update all rows
	$sKey = "";
	for ($i = 0; $i < $nKeySelected; $i++) {
		$sThisKey = trim($arRecKeys[$i]);
		$sKeyFld = $sThisKey;
		if (!is_numeric($sKeyFld)) {
			return FALSE;
		}
		$tb_locations->ID->CurrentValue = $sKeyFld;
		$tb_locations->SendEmail = FALSE; // Do not send email on update success
		$UpdateRows = EditRow(); // Update this row
		if (!$UpdateRows) break; // Update failed
		if ($sKey <> "") $sKey .= ", ";
		$sKey .= $sThisKey;
	}

	// Check if all rows updated
	if ($UpdateRows) {
		$conn->CommitTrans(); // Commit transaction
	} else {
		$conn->RollbackTrans(); // Rollback transaction
	}
	return $UpdateRows;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $tb_locations;
	$tb_locations->LocationName->setFormValue($objForm->GetValue("x_LocationName"));
	$tb_locations->LocationName->MultiUpdate = $objForm->GetValue("u_LocationName");
	$tb_locations->LocationRent->setFormValue($objForm->GetValue("x_LocationRent"));
	$tb_locations->LocationRent->MultiUpdate = $objForm->GetValue("u_LocationRent");
	$tb_locations->PayRoll->setFormValue($objForm->GetValue("x_PayRoll"));
	$tb_locations->PayRoll->MultiUpdate = $objForm->GetValue("u_PayRoll");
	$tb_locations->Utilities->setFormValue($objForm->GetValue("x_Utilities"));
	$tb_locations->Utilities->MultiUpdate = $objForm->GetValue("u_Utilities");
}

// Restore form values
function RestoreFormValues() {
	global $tb_locations;
	$tb_locations->LocationName->CurrentValue = $tb_locations->LocationName->FormValue;
	$tb_locations->LocationRent->CurrentValue = $tb_locations->LocationRent->FormValue;
	$tb_locations->PayRoll->CurrentValue = $tb_locations->PayRoll->FormValue;
	$tb_locations->Utilities->CurrentValue = $tb_locations->Utilities->FormValue;
}
?>
<?php

// Load recordset
function LoadRecordset($offset = -1, $rowcnt = -1) {
	global $conn, $tb_locations;

	// Call Recordset Selecting event
	$tb_locations->Recordset_Selecting($tb_locations->CurrentFilter);

	// Load list page sql
	$sSql = $tb_locations->SelectSQL();
	if ($offset > -1 && $rowcnt > -1) $sSql .= " LIMIT $offset, $rowcnt";

	// Load recordset
	$conn->raiseErrorFn = 'ew_ErrorFn';	
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';

	// Call Recordset Selected event
	$tb_locations->Recordset_Selected($rs);
	return $rs;
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $tb_locations;

	// Call Row Rendering event
	$tb_locations->Row_Rendering();

	// Common render codes for all row types
	// LocationName

	$tb_locations->LocationName->CellCssStyle = "";
	$tb_locations->LocationName->CellCssClass = "";

	// LocationRent
	$tb_locations->LocationRent->CellCssStyle = "";
	$tb_locations->LocationRent->CellCssClass = "";

	// PayRoll
	$tb_locations->PayRoll->CellCssStyle = "";
	$tb_locations->PayRoll->CellCssClass = "";

	// Utilities
	$tb_locations->Utilities->CellCssStyle = "";
	$tb_locations->Utilities->CellCssClass = "";
	if ($tb_locations->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($tb_locations->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($tb_locations->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// LocationName
		$tb_locations->LocationName->EditCustomAttributes = "";
		$tb_locations->LocationName->EditValue = ew_HtmlEncode($tb_locations->LocationName->CurrentValue);

		// LocationRent
		$tb_locations->LocationRent->EditCustomAttributes = "";
		$tb_locations->LocationRent->EditValue = $tb_locations->LocationRent->CurrentValue;

		// PayRoll
		$tb_locations->PayRoll->EditCustomAttributes = "";
		$tb_locations->PayRoll->EditValue = $tb_locations->PayRoll->CurrentValue;

		// Utilities
		$tb_locations->Utilities->EditCustomAttributes = "";
		$tb_locations->Utilities->EditValue = $tb_locations->Utilities->CurrentValue;
	} elseif ($tb_locations->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$tb_locations->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $tb_locations;
	$sFilter = $tb_locations->SqlKeyFilter();
	if (!is_numeric($tb_locations->ID->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@ID@", ew_AdjustSql($tb_locations->ID->CurrentValue), $sFilter); // Replace key value
	$tb_locations->CurrentFilter = $sFilter;
	$sSql = $tb_locations->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field LocationName
		if ($tb_locations->LocationName->MultiUpdate == "1") {
		$tb_locations->LocationName->SetDbValueDef($tb_locations->LocationName->CurrentValue, NULL);
		$rsnew['LocationName'] =& $tb_locations->LocationName->DbValue;
		}

		// Field LocationRent
		if ($tb_locations->LocationRent->MultiUpdate == "1") {
		$tb_locations->LocationRent->SetDbValueDef($tb_locations->LocationRent->CurrentValue, NULL);
		$rsnew['LocationRent'] =& $tb_locations->LocationRent->DbValue;
		}

		// Field PayRoll
		if ($tb_locations->PayRoll->MultiUpdate == "1") {
		$tb_locations->PayRoll->SetDbValueDef($tb_locations->PayRoll->CurrentValue, NULL);
		$rsnew['PayRoll'] =& $tb_locations->PayRoll->DbValue;
		}

		// Field Utilities
		if ($tb_locations->Utilities->MultiUpdate == "1") {
		$tb_locations->Utilities->SetDbValueDef($tb_locations->Utilities->CurrentValue, NULL);
		$rsnew['Utilities'] =& $tb_locations->Utilities->DbValue;
		}

		// Call Row Updating event
		$bUpdateRow = $tb_locations->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($tb_locations->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($tb_locations->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $tb_locations->CancelMessage;
				$tb_locations->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$tb_locations->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
